package searchteam.beans.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.*;

import searchteam.client.beansMirrors.PersonMirror;
import searchteam.client.beansMirrors.TeamMirror;

@Entity
@Table(name="TEAMS")
public class Team implements Serializable {

	private static final long serialVersionUID = -634220655293927798L;
	
	private int id;
	private String name;
	private String meaning;
	private String status;
	private Person teamMaster;
	private Collection<Person> members;

	private TeamMirror loaded;
	
	public Team() {
		members = new ArrayList<Person>();
	}
	
	public Team(String name, String meaning, String status, Person teamMaster) {
		this.name = name;
		this.meaning = meaning;
		this.status = status;
		this.teamMaster = teamMaster;
		members = new ArrayList<Person>();
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getMeaning() {
		return meaning;
	}

	public void setMeaning(String meaning) {
		this.meaning = meaning;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@OneToOne
	public Person getTeamMaster() {
		return teamMaster;
	}

	public void setTeamMaster(Person teamMaster) {
		this.teamMaster = teamMaster;
	}

	@OneToMany(mappedBy="team")
	public Collection<Person> getMembers() {
		return members;
	}

	public void setMembers(Collection<Person> members) {
		this.members = members;
	}

	public void addMember(Person member) {
		members.add(member);
	}
	
	@Transient
	public TeamMirror getMirror() {
		if(loaded == null) {
			loaded = new TeamMirror();
			loaded.setId(id);
			loaded.setMeaning(meaning);
			loaded.setName(name);
			loaded.setStatus(status);
			
			if(teamMaster != null) {
				loaded.setTeamMaster(teamMaster.getMirror());
			}
			
			Collection<PersonMirror> memberMirrors = new ArrayList<PersonMirror>();
			for(Person member : members) {
				memberMirrors.add(member.getMirror());
			}
			loaded.setMembers(memberMirrors);
		}
		
		return loaded;
	}
	
}
